﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using HZ.Supervise.Model;
using System.Text.RegularExpressions;

namespace HZ.Supervise.Web.Secondphase.ReportHospital
{
    public partial class hospitalGoodsDisable :BasePage
    {
        DataTable dt = new DataTable();
        int totalrecord = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dataBind();
            }
        }
        public hospitalGoodsDisable() 
        {
            this.ModuleNo = "特殊权限";
            this.PageOperateType = "锁定批号";
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            IList<HospitalGoodsDisable> hospitalGoodsDisableList = new List<HospitalGoodsDisable>();
            string goodsName = "";
            IBLL.IBLL_SpecialAuthority bll_special = new BLL.BLL_SpecialAuthority();
            DataTable dt =bll_special.findAllHospitalGoodsDisable();
            string BatchRecordIDs = "";
            string goodsIds = "";
            for (int i = 0; i < gvwProduceCatalog.Rows.Count; i++)
            {
                CheckBox checkbox = ((CheckBox)gvwProduceCatalog.Rows[i].Cells[0].FindControl("chk"));
                if (checkbox.Checked)
                {
                    HospitalGoodsDisable hospitalgoods = new HospitalGoodsDisable();
                    string goodsId=gvwProduceCatalog.Rows[i].Cells[1].Text;
                    hospitalgoods.GoodsID = gvwProduceCatalog.Rows[i].Cells[1].Text;
                    goodsIds += gvwProduceCatalog.Rows[i].Cells[1].Text+",";
                    TextBox textbox=(TextBox)gvwProduceCatalog.Rows[i].Cells[12].FindControl("txtCompanyName_SC");
                    string strBatchRecordID = textbox.Text;
                    goodsName +="通用名：" +gvwProduceCatalog.Rows[i].Cells[2].Text + "。批号：" + strBatchRecordID + "；";
                    if (string.IsNullOrEmpty(strBatchRecordID))
                    {
                        this.Page.RegisterStartupScript("", "<script>alert('请填写药品批号!')</script>");
                        return;
                    }
                    if (!string.IsNullOrEmpty(strBatchRecordID))
                    {
                        if (!IsBatchRecordID(strBatchRecordID))
                        {
                            this.Page.RegisterStartupScript("", "<script>alert('批号为不小于6位非汉字字符!')</script>");
                            return;
                        }
                    }

                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (goodsId.Equals(dt.Rows[j]["GoodsID"].ToString()) && strBatchRecordID.Equals(dt.Rows[j]["BatchRecordID"].ToString()))
                        {
                            this.Page.RegisterStartupScript("", "<script>alert('该批号的药品已经禁用过，不能重复禁用!')</script>");
                            return;
                        }
                    }
                    hospitalgoods.BatchRecordID=textbox.Text.Trim();
                    BatchRecordIDs+= textbox.Text.Trim() + ",";
                    hospitalGoodsDisableList.Add(hospitalgoods);
                }
            }
         
            int res = bll_special.InsertHospitalGoodsDisable(hospitalGoodsDisableList);
            HospitalGoodsDisableLog hospitalGoodsLog = new HospitalGoodsDisableLog();
            hospitalGoodsLog.InforMation = "<B>" + CurrentAccount.GetUser().UserID + "在" + DateTime.Now.ToString() + "禁用了药品批号，药品名称如下：</B></br>" + goodsName.TrimEnd(',') + "</br>";
            hospitalGoodsLog.Id = Guid.NewGuid().ToString();
            hospitalGoodsLog.ExecutionMen = CurrentAccount.GetUser().UserID;
            hospitalGoodsLog.IsDisable = 0;
            hospitalGoodsLog.GoodsID = goodsIds;
            hospitalGoodsLog.BatchRecordID = BatchRecordIDs;
            bll_special.InsertHospitalGoodsDisableLog(hospitalGoodsLog);
            if (res > 0)
            {
                this.Page.RegisterStartupScript("", "<script>alert('禁用药品批号成功!')</script>");
                dataBind();
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('禁用启用药品批号失败!')</script>");
            }
        }
        public static bool IsBatchRecordID(string s)// /^.*([A-Za-z]).*\1.*$/i 
        {
            Regex r = new Regex(@"^([A-Za-z0-9\-]{6,}?)$");
            return r.IsMatch(s);
        }
        private void dataBind()
        {
            StringBuilder where = new StringBuilder();

            if (!string.IsNullOrEmpty(txtProductName.Text.Trim()))
            {
                //中文按Produtname查询，英文按照PinyinCode查询
                if (HZ.Common.zhHansUtil.HaveChineseCode(this.txtProductName.Text.Trim()))
                {
                    where.Append(" and ProductName like '%" + txtProductName.Text.Trim() + "%'");
                }
                else
                {
                    where.Append(" and PinyinCode like '%" + txtProductName.Text.Trim() + "%'");
                }
            }
            if (!string.IsNullOrEmpty(txtMedicineModelName.Text.Trim()))
            {
                where.Append(" and MedicineModelName = '" + txtMedicineModelName.Text.Trim() + "'");
            }
            if (ddlSortSource.SelectedValue != "-1")
            {
                where.Append(" and SortSource = '" + ddlSortSource.SelectedValue + "'");
            }
            if (!string.IsNullOrEmpty(txtOutlookc.Text.Trim()))
            {
                where.Append(" and Outlookc like  '%" + txtOutlookc.Text.Trim() + "%'");
            }
            if (!string.IsNullOrEmpty(txtUnit.Text.Trim()))
            {
                where.Append(" and Unit = '" + txtUnit.Text.Trim() + "'");
            }
            if (!string.IsNullOrEmpty(txtCompanyName_SC.Text.Trim()))
            {
                where.Append(" and CompanyName_SC like '%" + txtCompanyName_SC.Text.Trim() + "%'");
            }
            HZ.Supervise.IBLL.IBLL_SpecialAuthority sp = HZ.Supervise.BLLFactory.IBLL_SpecialAuthority();
            dt = sp.findProcureCatalog(where.ToString(), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalrecord);
            gvwProduceCatalog.DataSource = dt;
            gvwProduceCatalog.DataBind();
            AspNetPager1.RecordCount = totalrecord;
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            dataBind();
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            dataBind();
        }
    }
}
